package com.macaline.util;


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * excel文件工具类
 */
public class ExcelUtil {


    public static void main(String[] args) {
        LinkedList<String> vector = new LinkedList<>();
        vector.add("1");
        vector.add("1");
        vector.add("1");
        LinkedList<String> vector2 = new LinkedList<>();
        vector2.add("1");
        vector2.add("1");
        vector2.add("1");
        LinkedList<LinkedList<String>> vectors = new LinkedList<LinkedList<String>>();
        vectors.add(vector);
        vectors.add(vector2);
        try {
            boolean excel = createExcel("C:\\Users\\think\\Desktop\\1.xlsx", vectors);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 创建
     * @param path
     * @param dataList
     * @return
     * @throws IOException
     */
    public static boolean createExcel(String path, LinkedList<LinkedList<String>> dataList) throws IOException {
        // 声明一个工作薄
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 单元格样式
        XSSFCellStyle style = workbook.createCellStyle();
        setupDefaultStyle(workbook, style);
        // 标题行样式
        XSSFCellStyle header = workbook.createCellStyle();
        header.setBorderBottom(BorderStyle.THIN);
        header.setBorderLeft(BorderStyle.THIN);
        header.setBorderRight(BorderStyle.THIN);
        header.setBorderTop(BorderStyle.THIN);
        header.setAlignment(HorizontalAlignment.LEFT);
        header.setVerticalAlignment(VerticalAlignment.CENTER);
        header.setWrapText(false);
        header.setFillForegroundColor(IndexedColors.AQUA.getIndex());
        header.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet("学校审核点预约名单");
        // 默认列宽
        sheet.setDefaultColumnWidth(15);
        // 默认行高
        sheet.setDefaultRowHeightInPoints(15);
        // 保证原子性的int包装类。
        AtomicInteger rowIndex = new AtomicInteger();
        AtomicInteger cellIndex = new AtomicInteger();
        AtomicReference<XSSFRow> row = new AtomicReference<XSSFRow>();
        AtomicReference<XSSFCell> cell = new AtomicReference<XSSFCell>();

        // 循环遍历行
        dataList.forEach(rowItem -> {
            cellIndex.set(0);
            row.set(sheet.createRow(rowIndex.get()));
            // 循环遍历列
            rowItem.forEach(cellItem -> {
                cell.set(row.get().createCell(cellIndex.get()));
                cell.get().setCellValue(cellItem);
                // +1操作
                cellIndex.getAndIncrement();
            });
            // +1操作
            rowIndex.getAndIncrement();
        });

        FileOutputStream o = new FileOutputStream(path);
        workbook.write(o);
        if (o != null) {
            o.flush();
            o.close();
        }
        workbook.close();
        return true;
    }

    /**
     * 表格默认样式
     */
    @SuppressWarnings("deprecation")
    private static void setupDefaultStyle(XSSFWorkbook workbook, XSSFCellStyle style) {
        // 设置单元格边线
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        // 设置单元格内容水平对其方式
        style.setAlignment(HorizontalAlignment.LEFT);
        // 设置单元格内容垂直对其方式
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置单元格内容是否自动换行
        style.setWrapText(true);
        // 格式：文本
        XSSFDataFormat cdf = workbook.createDataFormat();
        style.setDataFormat(cdf.getFormat("@"));
        // 生成一个字体
        XSSFFont font = workbook.createFont();
        font.setFontName("Courier New");// 字体
        font.setFontHeightInPoints((short) 10);
        // 把字体应用到当前的样式
        style.setFont(font);
    }

}
